#include "ai_test.h"
#include "info.h"
#include "random.h"
#include "pokermath.h"
#include <iostream>

Action AITest::doTurn(const Info& info)
{
  double raiseChance = 0.0;
  double callChance = 0.0;
   double foldChance = 0.0;
  double allInChance = 0.0;
  int maxWager = 0; //for calling

  int bb = info.getBigBlind();

//na preflopie jak smart
  if(info.round == R_PRE_FLOP)
  {
    int group = getSklanskyMalmuthGroup(info.getHoleCards()[0], info.getHoleCards()[1]);
    if(group == 9) maxWager = 0;
    else if(group == 8 || group == 7) maxWager = bb;
    else if(group == 6 || group == 5)
    {
      maxWager = bb * 4;
      raiseChance = 0.2;
    }
    else if(group == 4 || group == 3)
    {
      maxWager = info.getStack() + info.getWager();
      raiseChance = 0.5;
      allInChance = 0.1;
    }
    else if(group == 2 || group == 1)
    {
      maxWager = info.getStack() + info.getWager();
      raiseChance = 0.5;
      allInChance = 0.2;
    }
    
    int minWagerTotal = info.getWager() + info.getCallAmount();
  if(minWagerTotal > info.getWager() + info.getStack()) minWagerTotal = info.getWager() + info.getStack();

  if(minWagerTotal > maxWager)
  {
    return info.getCheckFoldAction();
  }
  else
  {
    if(getRandom() < raiseChance)
    {
      int amount = (int)(info.getMinChipsToRaise() * (1.0 + getRandom()));

      //round the amount to become a multiple of big blinds
      amount = (amount / bb) * bb;
      if(amount == 0) amount = info.getMinChipsToRaise();

      return info.amountToAction(amount);
    }
    else if(getRandom() < allInChance)
    {
      return info.getAllInAction();
    }
    else
    {
      return info.getCallAction();
    }
  }

  return info.getCheckFoldAction();
  }
  else
  {
    double win, tie, lose;
    int num_opponents = info.getNumActivePlayers() - 1;
    double potodds = info.getPotOddsPercentage();
    bool committed = info.turn>1;
    
    if(info.round == R_FLOP) 
    {
        getWinChanceAgainstNAtFlop(win, tie, lose, info.getHoleCards()[0], info.getHoleCards()[1], info.boardCards[0], info.boardCards[1], info.boardCards[2], num_opponents);
        if(win >= lose)
        {
            if(potodds < 0.1 && committed)
            {
                int amount = (int)(info.getMinChipsToRaise() * (1.0 + getRandom()));
                return info.amountToAction(amount);
            }
            return info.getCallAction();
        }
        else
        {
            return info.getCheckFoldAction();
        }
    }
    else if(info.round == R_TURN) 
    {
        getWinChanceAgainstNAtTurn(win, tie, lose, info.getHoleCards()[0], info.getHoleCards()[1], info.boardCards[0], info.boardCards[1], info.boardCards[2], info.boardCards[3], num_opponents);
        if(win >= lose)
        {
            if(potodds < 0.2)
            {
                int amount = (int)(info.getMinChipsToRaise() * (1.0 + getRandom()));
                return info.amountToAction(amount);
            }
            return info.getCallAction();
        }
        else
        {
            return info.getCallAction();
        }
    }    
    else if(info.round == R_RIVER) 
    {
        getWinChanceAgainstNAtRiver(win, tie, lose, info.getHoleCards()[0], info.getHoleCards()[1], info.boardCards[0], info.boardCards[1], info.boardCards[2], info.boardCards[3], info.boardCards[4], num_opponents);
        if(win > lose && committed) return info.getAllInAction();
        int amount = (int)(info.getMinChipsToRaise() * (1.0 + getRandom()));
        return info.amountToAction(amount);
    }   
  }  
}


std::string AITest::getAIName()
{
  return "Test";
}
void AITest::addPlayerStats(ObserverStatKeeper* observer)
{
}
